package com.medical.sys.service.impl;

import com.medical.common.TreeNode;
import com.medical.sys.mapper.ResourceRoleMapper;
import com.medical.sys.mapper.RoleMapper;
import com.medical.sys.mapper.UserMapper;
import com.medical.sys.model.Role;
import com.medical.sys.model.User;
import com.medical.sys.service.RoleService;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@SuppressWarnings("SpringJavaAutowiringInspection")
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private ResourceRoleMapper resourceRoleMapper;
    @Autowired
    private UserMapper userMapper;

    public Role selectByPrimaryKey(String id){
        return roleMapper.selectByPrimaryKey(id);
    }

    public List<Role> getRoles(){
        return getRoles(false);
    }

    public List<Role> getRoles(Boolean administrator) {
        return roleMapper.selectAll(administrator);
    }

    public void saveRole(Role role) {
        if(role != null){
            if(StringUtils.isNullOrEmpty(role.getId())){
                role.setId(UUID.randomUUID().toString());
            }
            for(String resourceId : role.getResourceIds()){
                resourceRoleMapper.add(resourceId, role.getId());
            }
            for(String userId : role.getUserIds()){
                User user = userMapper.selectByPrimaryKey(userId);
                if(user != null) {
                    user.getRoleIds().add(role.getId());
                    userMapper.updateByPrimaryKey(user);
                }
            }
            roleMapper.insert(role);
        }
    }
    public Role getRole(String roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }

    public void deleteRole(String roleId){
        deleteRole(roleId,false);//默认标记删除
    }

    public void deleteRole(String roleId, Boolean administrator) {
        if(administrator){//系统管理员删除记录
            roleMapper.deleteByPrimaryKey(roleId);
        }else{//非系统管理员标记删除
            Role role = roleMapper.selectByPrimaryKey(roleId);
            if(role != null){
                role.setDeleted(true);
                roleMapper.updateByPrimaryKey(role);
            }
        }
    }
    public void updateRole(Role role) {
        resourceRoleMapper.remove(null, role.getId());
        for(String resourceId : role.getResourceIds()){
            resourceRoleMapper.add(resourceId, role.getId());
        }
        for(String userId : role.getUserIds()){
            User user = userMapper.selectByPrimaryKey(userId);
            if(user != null) {
                Boolean exist = false;
                for(String roleId : user.getRoleIds()){//检查该用户之前是否拥有该角色
                    if(roleId.equals(role.getId())){
                        exist = true;
                        break;
                    }
                }
                if(!exist) {//不存在即添加
                    user.getRoleIds().add(role.getId());
                    userMapper.updateByPrimaryKey(user);
                }
            }
        }
        roleMapper.updateByPrimaryKey(role);
    }
}

